package com.htkj.demo

import android.content.Context
import android.os.Parcelable
import com.tencent.mmkv.MMKV

object CacheUtil {

    val mmkv by lazy {
        MMKV.mmkvWithID("goday")
    }
    fun init(base: Context){
        MMKV.initialize(base.filesDir.absolutePath + "/mmkv")
    }



    fun setString(key:String,volue:String){
        mmkv.encode(key,volue)
    }

    fun getString(key:String,defaultValue:String?=""):String{
       return mmkv.decodeString(key,defaultValue)
    }

    fun getString(key: String): String {
        return getString(key,"")
    }

    fun setInt(key:String,volue:Int){
        mmkv.encode(key,volue)
    }


    fun getInt(key:String,defaultValue:Int?=0):Int{
        return mmkv.decodeInt(key,defaultValue?:0)
    }


    fun setZoneID(volue:Int){
        mmkv.encode("Zone",volue)
    }


    fun getZoneID():Int{
        return mmkv.decodeInt("Zone",0)
    }



    fun <T : Parcelable> encode(key: String, t: T?) {
        if(t ==null){
            mmkv.removeValueForKey(key)
            return
        }
        mmkv?.encode(key, t)
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return mmkv?.decodeParcelable(key, tClass)
    }



}